package com.aso114.project.base;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.WindowManager;

import com.aso114.project.util.ActivityCollector;
import com.aso114.project.util.Helper;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * username:Created by hsj on 2016/10/11 0011.
 * class：
 * 类描述：
 */

public abstract class BaseMvpActivity<P extends Presenter<V>, V extends BaseMvpView> extends AppCompatActivity implements BaseMvpView, LoaderManager.LoaderCallbacks<P> {
    private final int BASE_LODER_ID = 1000;//loader的id值
    protected P presenter;

    protected abstract int getResourcesId();

    protected abstract void initView();

    protected abstract void addOnClickListener();

    /**
     * @param context 获取广播
     * @param intent  获取广播数据
     */
    protected abstract void getBroadcast(Context context, Intent intent);

    private boolean isFirst = true;


    protected IntentFilter filter = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        setContentView(getResourcesId());
        ButterKnife.bind(this);
        Log.d("info", getClass().getSimpleName());
        getSupportLoaderManager().initLoader(BASE_LODER_ID, null, this);//初始化loader
        ActivityCollector.addActivity(this);
        ActivityCollector.setCurrentActivity(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.attachView((V) this);//presenter与view 连接
        if (isFirst) {
            initView();
            addOnClickListener();
            isFirst = false;
        }

    }

    @Override
    public void showErr(String err) {
        Helper.showToast(err);
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void onLoadFinished(Loader<P> loader, P data) {
        presenter = data;
    }

    @Override
    public void onLoaderReset(Loader<P> loader) {
        presenter = null;
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        presenter.detachView();//presenter与view 断开连接
        super.onDestroy();
        ActivityCollector.reomveActivity(this);
    }

    protected void startActivity(Class<?> tClass, boolean isFinish) {
        Intent intent = new Intent(this, tClass);
        startActivity(intent);
        if (isFinish) {
            finish();
        }
    }

    /**
     * @param tClass   目标activity
     * @param bundle   携带数据
     * @param isFinish true 表示结束本界面
     */
    protected void startActivity(Class<?> tClass, Bundle bundle, boolean isFinish) {
        Intent intent = new Intent(this, tClass);
        intent.putExtras(bundle);
        startActivity(intent);
        if (isFinish) {
            finish();
        }
    }

    protected void startActivity(Class<?> tClass) {
        startActivity(tClass, false);
    }

    /**
     * @param action 广播action
     */
    public void addAction(String action) {
        filter = new IntentFilter(action);
        registerReceiver(mBroadcastReceiver, filter);
    }

    /**
     * 广播
     */
    public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            getBroadcast(context, intent);
        }
    };
}

